package com.baizhi.zjs.service;

import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.ibatis.session.SqlSession;

import com.baizhi.zjs.dao.BookDAO;
import com.baizhi.zjs.entity.BookEntity;
import com.baizhi.zjs.entity.CategoryEntity;
import com.baizhi.zjs.util.MybatisUtil;

public class BookServiceImpl implements BookService{

	@Override
	public void addBook(BookEntity book) {
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		BookDAO dao = sqlSession.getMapper(BookDAO.class);
		System.out.println("bookservice-------------");
		System.out.println(book);
		try {
			//创建SimpleDateFormat 对象
			//SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			
			//执行添加的方法
			dao.addBook(book);
			//提交事务
			MybatisUtil.commit(sqlSession);
			MybatisUtil.close(sqlSession);
		} catch (Exception e) {
			// TODO: handle exception
			//事务回滚
			MybatisUtil.rollback(sqlSession);
			MybatisUtil.close(sqlSession);
			e.printStackTrace();
			
		}
		
		
	}
//查询所有图书
	@Override
	public List<BookEntity> showAllBook() {
		// TODO Auto-generated method stub
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		BookDAO dao = sqlSession.getMapper(BookDAO.class);
		List<BookEntity> list = dao.showAllBook();
		MybatisUtil.close(sqlSession);
		return list;
	}
	//删除图书
	@Override
	public void deleteBook(String id) {
		// TODO Auto-generated method stub
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		BookDAO dao = sqlSession.getMapper(BookDAO.class);
		try {
			dao.delectBook(id);
			MybatisUtil.commit(sqlSession);
			MybatisUtil.close(sqlSession);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			MybatisUtil.rollback(sqlSession);
			MybatisUtil.close(sqlSession);
		}
	}
	//查询一条图书信息
	@Override
	public BookEntity shoeOneBook(String id) {
		// TODO Auto-generated method stub
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		BookDAO dao = sqlSession.getMapper(BookDAO.class);
		BookEntity book = dao.showOneBook(id);
		return book;
	}
	//修改图书信息
	@Override
	public void updateOneBook(BookEntity book) {
		// TODO Auto-generated method stub
	SqlSession sqlSession = MybatisUtil.getSqlSession();
	BookDAO dao = sqlSession.getMapper(BookDAO.class);
	try {
		dao.updateBook(book);
		MybatisUtil.commit(sqlSession);
		MybatisUtil.close(sqlSession);
		
	} catch (Exception e) {
		// TODO: handle exception
		MybatisUtil.rollback(sqlSession);
		MybatisUtil.rollback(sqlSession);
		e.printStackTrace();
	}
	}
	@Override
	public List<BookEntity> likeBook(BookEntity book) {
		// TODO Auto-generated method stub
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		BookDAO dao = sqlSession.getMapper(BookDAO.class);
		List<BookEntity> list = dao.likeBook(book);
		MybatisUtil.close(sqlSession);
		return list;
	}
	//前台热销图书
	@Override
	public List<BookEntity> fShowHotBook() {
		// TODO Auto-generated method stub
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		BookDAO dao = sqlSession.getMapper(BookDAO.class);
		List<BookEntity> list = dao.fShowHotBook();
		MybatisUtil.close(sqlSession);
		return list;
	}
	//前台最新上架
	@Override
	public List<BookEntity> fShowNewBook() {
		// TODO Auto-generated method stub
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		BookDAO dao = sqlSession.getMapper(BookDAO.class);
		List<BookEntity> list = dao.fShowNewBook();
		MybatisUtil.close(sqlSession);
		return list;
	}
	//前台新书热卖
	@Override
	public List<BookEntity> fShNewHotBook() {
		// TODO Auto-generated method stub
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		BookDAO dao = sqlSession.getMapper(BookDAO.class);
		List<BookEntity> list = dao.fShowNewHotBook();
		MybatisUtil.close(sqlSession);
		return list;
	}
	//前台编辑推荐
	@Override
	public List<BookEntity> fShowCompile() {
		// TODO Auto-generated method stub
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		BookDAO dao = sqlSession.getMapper(BookDAO.class);
		List<BookEntity> list = dao.fShowCompile();
		MybatisUtil.close(sqlSession);
		return list;
	}
	@Override
	public List<BookEntity> queryOneCeateBook(String id, String id1,Integer pageNum,Integer pageSize) {
		// TODO Auto-generated method stub
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		BookDAO dao = sqlSession.getMapper(BookDAO.class);
		
	//  当前页 pageNum  每页展示条数：pageSize  分页   3
			//1 1~3
			//2 4~6
			//3 7~9
			//  (pageNum-1)*pageSize+1   ~  pageNum*pageSize

			// rownum 开始条数  结束条数     
			Integer begin=(pageNum-1)*pageSize+1;
			Integer end=pageNum*pageSize;
	
		List<BookEntity> ca= dao.queryOneCeateBook(id,id1,begin,end);
		return ca;
	}
	@Override
	//前台：分页查询：根据类别查询图书数量
	public Integer queryByCount(String id, String id1) {
		// TODO Auto-generated method stub
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		BookDAO dao = sqlSession.getMapper(BookDAO.class);
		Integer count = dao.queryByCount(id, id1);
		MybatisUtil.close(sqlSession);
		return count;
	}
	@Override
	public Integer queryByPage(String id, String id1, Integer pageSize) {
		SqlSession sqlSession = MybatisUtil.getSqlSession();
		BookDAO dao = sqlSession.getMapper(BookDAO.class);
		// TODO Auto-generated method stub
		System.out.println(id);
		System.out.println(id1);
		Integer count = dao.queryByCount(id, id1);
		System.out.println("service1======"+count);
		//总页数     总条数/每页展示条数
		Integer pageTotal=count%pageSize==0?count/pageSize:count/pageSize+1;
		System.out.println("service层=====================");
	System.out.println(pageTotal);
		//返回总页数
		return pageTotal;
	}

}
